
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[Categories_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[Categories_Insert]

IF OBJECT_ID(N'[dbo].[Categories_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[Categories_Update]

IF OBJECT_ID(N'[dbo].[Categories_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[Categories_Delete]

IF OBJECT_ID(N'[dbo].[Categories_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[Categories_Select]

--endregion

GO


--region [dbo].[Categories_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Categories_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdCategory int = null,
	@Title nvarchar(1000) = null,
	@IsActived bit = null,
	@CreatedDate datetime = null,
	@LastModifiedDate datetime = null,
	@IdParent int = null,
	@Description nvarchar(max) = null,
	@SortOrder int = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[Categories]
					WHERE
					(
						(@IdCategory is null OR [Categories].[IdCategory] = @IdCategory)
						AND (@Title is null OR [Categories].[Title] like @Title)
						AND (@IsActived is null OR [Categories].[IsActived] = @IsActived)
						AND (@CreatedDate is null OR [Categories].[CreatedDate] = @CreatedDate)
						AND (@LastModifiedDate is null OR [Categories].[LastModifiedDate] = @LastModifiedDate)
						AND (@IdParent is null OR [Categories].[IdParent] = @IdParent)
						AND (@Description is null OR [Categories].[Description] like @Description)
						AND (@SortOrder is null OR [Categories].[SortOrder] = @SortOrder)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[Categories].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdCategory' and @SortType = 1 THEN [Categories].[IdCategory] END ASC,
						CASE WHEN @SortBy = 'IdCategory' and @SortType = 0 THEN [Categories].[IdCategory] END DESC,
						CASE WHEN @SortBy = 'Title' and @SortType = 1 THEN [Categories].[Title] END ASC,
						CASE WHEN @SortBy = 'Title' and @SortType = 0 THEN [Categories].[Title] END DESC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 1 THEN [Categories].[IsActived] END ASC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 0 THEN [Categories].[IsActived] END DESC,
						CASE WHEN @SortBy = 'CreatedDate' and @SortType = 1 THEN [Categories].[CreatedDate] END ASC,
						CASE WHEN @SortBy = 'CreatedDate' and @SortType = 0 THEN [Categories].[CreatedDate] END DESC,
						CASE WHEN @SortBy = 'LastModifiedDate' and @SortType = 1 THEN [Categories].[LastModifiedDate] END ASC,
						CASE WHEN @SortBy = 'LastModifiedDate' and @SortType = 0 THEN [Categories].[LastModifiedDate] END DESC,
						CASE WHEN @SortBy = 'IdParent' and @SortType = 1 THEN [Categories].[IdParent] END ASC,
						CASE WHEN @SortBy = 'IdParent' and @SortType = 0 THEN [Categories].[IdParent] END DESC,
						CASE WHEN @SortBy = 'Description' and @SortType = 1 THEN [Categories].[Description] END ASC,
						CASE WHEN @SortBy = 'Description' and @SortType = 0 THEN [Categories].[Description] END DESC,
						CASE WHEN @SortBy = 'SortOrder' and @SortType = 1 THEN [Categories].[SortOrder] END ASC,
						CASE WHEN @SortBy = 'SortOrder' and @SortType = 0 THEN [Categories].[SortOrder] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[Categories]
			Where 
			(
				(@IdCategory is null OR [Categories].[IdCategory] = @IdCategory)
				AND (@Title is null OR [Categories].[Title] like @Title)
				AND (@IsActived is null OR [Categories].[IsActived] = @IsActived)
				AND (@CreatedDate is null OR [Categories].[CreatedDate] = @CreatedDate)
				AND (@LastModifiedDate is null OR [Categories].[LastModifiedDate] = @LastModifiedDate)
				AND (@IdParent is null OR [Categories].[IdParent] = @IdParent)
				AND (@Description is null OR [Categories].[Description] like @Description)
				AND (@SortOrder is null OR [Categories].[SortOrder] = @SortOrder)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[Categories_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[Categories_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Categories_Insert]
	@IdCategory int OUTPUT,
	@Title nvarchar(1000),
	@IsActived bit,
	@CreatedDate datetime,
	@LastModifiedDate datetime,
	@IdParent int,
	@Description nvarchar(max),
	@SortOrder int

AS


INSERT INTO [dbo].[Categories] 
(
	[Title],
	[IsActived],
	[CreatedDate],
	[LastModifiedDate],
	[IdParent],
	[Description],
	[SortOrder]
)
VALUES 
(
	@Title,
	@IsActived,
	@CreatedDate,
	@LastModifiedDate,
	@IdParent,
	@Description,
	@SortOrder
)

SET @IdCategory = SCOPE_IDENTITY()

--end [dbo].[Categories_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[Categories_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Categories_Update]
	@IdCategory int,
	@Title nvarchar(1000),
	@IsActived bit,
	@CreatedDate datetime,
	@LastModifiedDate datetime,
	@IdParent int,
	@Description nvarchar(max),
	@SortOrder int
AS


UPDATE [dbo].[Categories] SET
	[Title] = @Title,
	[IsActived] = @IsActived,
	[CreatedDate] = @CreatedDate,
	[LastModifiedDate] = @LastModifiedDate,
	[IdParent] = @IdParent,
	[Description] = @Description,
	[SortOrder] = @SortOrder
WHERE
	[IdCategory] = @IdCategory

--end [dbo].[Categories_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[Categories_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Categories_Delete]
	@IdCategory int
AS


DELETE FROM [dbo].[Categories]
WHERE
(
	[IdCategory] = @IdCategory
)

--end [dbo].[Categories_Delete]
--endregion

GO
--=========================================================================================--

